import knex from "knex";
import { Datasource_dto } from "../models/datasource.dto.js";
import configDefault from "./config.default.js";

var datasources = configDefault?.datasources as unknown as { default: Datasource_dto };

export function initdb_1() {
  const myDBConfig = {
    debug: true,
    useNullAsDefault: true,
    client: "better-sqlite3",
    connection: {
      filename: "myEncryptedSQLiteDbFile.db"
    },
    pool: { min: 0, max: 7 }
  };
  var k = knex(myDBConfig);

  return k;
}


export function init_systemdb() {
  const myDBConfig = {
    debug: true,
    client: "mysql2",
    connection: {
      host: datasources?.default?.host || "192.168.11.204",
      port: datasources?.default?.port || 13306,
      user: datasources?.default?.user || "root",
      password: datasources?.default?.password || "15s6f9E3613f9k",
      database: datasources?.default?.database || "szq_system"
    },
    pool: { min: 0, max: 7 }
  };
  var a = (knex(myDBConfig));
  return a;
}

export function getdb(options: Datasource_dto) {
  var myDBConfig = undefined;
  if (options?.type == "sqlite") {
    myDBConfig = {
      debug: true,
      useNullAsDefault: true,
      client: "better-sqlite3",
      connection: {
        filename: options?.host
      },
      pool: { min: 0, max: 7 }
    };
    var a = (knex(myDBConfig));
    return a;
  }

  myDBConfig = {
    debug: true,
    client: "mysql2",
    connection: {
      host: options.host || "192.168.11.204",
      port: options.port || 13306,
      user: options.user || "root",
      password: options.password || "15s6f9E3613f9k",
      database: options.database || "szq_system"
    },
    pool: { min: 0, max: 7 }
  };
  var a = (knex(myDBConfig));

  return a;
}

export function getsqlit_db(options: { filepathandfilename }) {
  const myDBConfig = {
    debug: true,
    useNullAsDefault: true,
    client: "better-sqlite3",
    connection: {
      filename: options?.filepathandfilename
    },
    pool: { min: 0, max: 7 }
  };
  var a = (knex(myDBConfig));

  return a;
}
